package com.controller;

import com.pojo.book;
import com.service.userService;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class showUserInfoServlet extends HttpServlet {
    private userService userService=new userService();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        Cookie[] cookies = req.getCookies();
        PrintWriter out = resp.getWriter();
        String uid = null;
        double userMoney=0.0;
        HttpSession session = req.getSession();
        Map<String, List<book>> cars = (Map<String, List<book>>) session.getAttribute("car");
        for (int i = 0; i < cookies.length; i++) {
            if (cookies[i].getName().equals("loginUser")) {
                uid = cookies[i].getValue();
                break;
            }
        }
        //查看用户还有多少余额
        if (uid != null) {
            int userid = Integer.parseInt(uid);
            try {
                userMoney = userService.getUserMoney(userid);
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            out.print("当前用户的余额为：" + userMoney + "元<br/>");
            out.print("<a href=\"index.jsp\">返回主页面</a>");

        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
